const Promise = require('bluebird');
const moment = require('moment');

/**
 * @api {get} /api/admin/v1/withdraws/analyse 提现/结算统计
 * @apiGroup AdminWithdraws
 * @apiDescription 财务管理-提现/结算统计
 * @apiPermission 管理员(查看资金流水权限)
 * @apiVersion 1.0.0
 */
exports.analyse = function ({}, {models: {UsersWithdraws}}) {
    return UsersWithdraws.analyseV2();
}

/**
 * @api {get} /api/admin/v1/withdraws/browse 提现/结算列表
 * @apiGroup AdminWithdraws
 * @apiDescription 财务管理-提现/结算列表
 * @apiPermission 管理员(查看资金流水权限)
 * @apiVersion 1.0.0
 */
exports.browse = function ({query}, {models: {UsersWithdraws, Users, associations}, cache: {settings}, controller}) {
    let pagination = controller.pagination({
        params: query,
        filters: ['partner_id', 'user_id', 'is_virtual', 'status', 'bank_type', 'bank_tel'],
        likes: ['trade_no', 'bank_name', 'bank_no', 'bank_tel']
    });
    return UsersWithdraws.findAndCountAll(pagination).then(function (data) {
        return associations.relateOne(data, Users, 'user_id', 'user', ['id', 'image', 'nickname', 'username', 'nickname', 'tel', 'level', 'order_count', 'finish_count', 'sham_count', 'amount', 'frozen']);
    }).then(function (data) {
        return associations.relateOne(data, Users, 'partner_id', 'partner_user', ['id', 'image', 'username', 'nickname', 'tel']);
    });
}

/**
 * @api {get} /api/admin/v1/withdraws/load 提现/结算详情
 * @apiGroup AdminWithdraws
 * @apiDescription 财务管理-提现/结算详情
 * @apiPermission 管理员(查看资金流水权限)
 * @apiVersion 1.0.0
 */
exports.load = function ({query}, {models: {UsersWithdraws, Users}, cache: {settings}}) {
    let {id} = query;
    return UsersWithdraws.findOne({where: {id: id}}).then(function (withdraw) {
        return Promise.props({
            user: Users.findOne({
                where: {id: withdraw.user_id},
                attributes: ['id', 'image', 'nickname', 'username', 'nickname', 'tel', 'level', 'order_count', 'finish_count', 'sham_count', 'amount', 'frozen']
            }),
            partner_user: Users.findOne({
                where: {id: withdraw.partner_id},
                attributes: ['id', 'image', 'nickname', 'username', 'nickname', 'tel']
            })
        }).then(function ({user, partner_user}) {
            withdraw.user = user;
            withdraw.partner_user = partner_user;
            return withdraw;
        });
    });
}

/**
 * @api {post} /api/admin/v1/withdraws/status 更改状态
 * @apiGroup AdminWithdraws
 * @apiDescription 财务管理-更改状态
 * @apiPermission 管理员(编辑资金流水权限)
 * @apiVersion 1.0.0
 *
 * @apiParam {Number}   id         编号
 * @apiParam {String}   status     状态
 *
 * @apiUse apiSuccessResult
 */
exports.status = function ({body, user_id}, {models: {UsersWithdraws}}) {
    let {id, status} = body;
    if (status === 'Successful') {
        return UsersWithdraws.successfulV2(id);
    } else if (status === 'Failed') {
        return UsersWithdraws.failedV2(id);
    } else {
        return {success: false};
    }
}

/**
 * @api {post} /api/admin/v1/withdraws/edit_note 编辑备注
 * @apiGroup AdminWithdraws
 * @apiDescription 财务管理-编辑备注
 * @apiPermission 管理员(编辑资金流水权限)
 * @apiVersion 1.0.0
 *
 * @apiParam {Number}   id         编号
 * @apiParam {String}   note       备注说明
 *
 * @apiUse apiSuccessResult
 */
exports.editNote = function ({body, user_id}, {models: {UsersWithdraws}}) {
    let {id, note} = body;
    return UsersWithdraws.update({note: note}, {where: {id: id}}).then(function (result) {
        return {success: (result[0] > 0)};
    });
}